package com.jcgyl.export.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jcgyl.export.entity.Tbsysuser;
import com.jcgyl.export.model.userinfo.UserQueryModel;
import com.jcgyl.export.service.UserInfoService;
import com.jcgyl.export.util.PasswordHelper;
import com.jcgyl.export.util.entity.JsonResult;
import com.jcgyl.export.util.entity.JsonUtil;
import com.jcgyl.export.util.entity.PageResult;


@Controller
@RequestMapping("/userinfo/")
public class UserInfoController {
	@Autowired
	private UserInfoService userInfoService;
	/**返回page页面*/
	@RequestMapping("index")
	public String page() {
		return "userinfo/index";
	}
	/**查询全部*/
	@RequestMapping("doFindObjects")
	@ResponseBody
	public PageResult doFindObjects(){
		List<Map<String,Object>>list = userInfoService.findObjects();
		int row = userInfoService.getRowCount();
		PageResult jsonResult = new PageResult();
		jsonResult.setResult(list);
		jsonResult.setTotalRows(row);
		return jsonResult;
	}
	/**
	 * 根据条件查询数据
	 * @param queryModel
	 * @return
	 */
	@RequestMapping("doFindObject")
	@ResponseBody
	public PageResult doFindObject(@RequestBody UserQueryModel queryModel){
		String departmentname = queryModel.getFilter().getDepartmentName();
		String name = queryModel.getFilter().getUname();
		Integer pageSize = queryModel.getPageSize();
		Integer pageCurrent = queryModel.getPageIndex();
		List<Map<String,Object>>list = userInfoService.findObject(departmentname, name, pageSize, pageCurrent);
		int row = userInfoService.getRow(departmentname, name);
		PageResult jsonResult = new PageResult();
		jsonResult.setResult(list);
		jsonResult.setTotalRows(row);
		return jsonResult;
	}
	/**
	 * @param tbsysuser
	 * @return
	 *   添加用户
	 */
	@RequestMapping("doSaveObject")
	@ResponseBody
	public JsonResult doSaveObject(@RequestBody Tbsysuser tbsysuser) {
		return JsonUtil.success(userInfoService.saveObject(tbsysuser));
	}
	
	/**根据id删除用户*/
	@RequestMapping("doDeleteObject")
	@ResponseBody
	public PageResult doDeleteObject(@RequestBody Integer uid) {
		Long id =(long)uid; 
		int row = userInfoService.deleteObject(id);
		PageResult jsonResult = new PageResult();
		jsonResult.setResult(row);
		return jsonResult;
	}
	/*修改用户*/
	@RequestMapping("doUpdataObject")
	@ResponseBody
	public PageResult doUpdataObject(@RequestBody Tbsysuser tbsysuser) {
		int row = userInfoService.updataObject(tbsysuser);
		PageResult jsonResult = new PageResult();
		jsonResult.setResult(row);
		return jsonResult;
	}
	/**
	 * 重置密码
	 * @param tbsysuser
	 * @return
	 * JL
	 */
	@RequestMapping("doUpdataPw")
	@ResponseBody
	public PageResult doUpdataPw(@RequestBody Tbsysuser tbsysuser){
		//Long id =(long)uid; 
		String uname = tbsysuser.getUname();
		System.out.println(tbsysuser.getUid());
		Tbsysuser sysuser= userInfoService.findByUserName(uname);
		String password =PasswordHelper.encryptPassword("123456", sysuser.getSalt());
		PageResult jsonResult = new PageResult();
		if(password!=sysuser.getPassword()) {
			//pw="123456";
			int row =userInfoService.resetpw(tbsysuser.getUid());
			jsonResult.setResult(row);
		}
		return jsonResult;
	}
	/**
	 * 根据名字查询数据
	 * @param name
	 * @return
	 *JL
	 */
	@RequestMapping("doFindName")
	@ResponseBody
	public PageResult doFindName(@RequestBody String name) {
		String uname = name;
		int row =userInfoService.findName(uname);
		PageResult jsonResult = new PageResult();
		jsonResult.setResult(row);
		return jsonResult;
	}
}
